     <template>
    <div class="add-and-edit" style="margin-top:20px">
        <el-button
            @click="$router.go(-1)"
            type="primary"
            style="padding:10px 40px;margin-left:40px;margin-bottom:20px"
        >返回上一页</el-button>
        <el-form
            :inline="false"
            :model="addForm"
            size="mini"
            class="demo-form-inline"
            label-width="120px"
            :rules="dataRule"
            ref="ruleForm"
        >
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="模块名称" prop="name">
                        <el-input v-model="addForm.name" placeholder="请输入模块名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="模块编码" prop="code">
                        <el-input v-model="addForm.code" placeholder="请输入模块名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="模块类型" prop="linkType">
                        <el-radio-group v-model="addForm.linkType">
                            <el-radio label="NATIVE">原生</el-radio>
                            <el-radio label="H5">H5</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="功能状态" prop="status">
                        <el-radio-group v-model="addForm.status">
                            <el-radio label="ON_SHELF">上架</el-radio>
                            <el-radio label="OFF_SHELF">下架</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="IOS图标" prop="iosIcon">
                        <upload :img.sync="addForm.iosIcon" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="安卓图标" prop="androidIcon">
                        <upload :img.sync="addForm.androidIcon" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="模块投放" prop="accountTypeSupport">
                        <el-checkbox-group v-model="addForm.accountTypeSupport">
                            <el-checkbox label="2">教师端</el-checkbox>
                            <el-checkbox label="1">学生端</el-checkbox>
                            <el-checkbox label="3">家长端</el-checkbox>
                        </el-checkbox-group>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="推送方式" prop="pushType">
                        <el-radio-group v-model="addForm.pushType">
                            <el-radio label="reddot">红点</el-radio>
                            <el-radio label="notice">通知</el-radio>
                            <el-radio label="sms">手机短信</el-radio>
                            <el-radio label="wechat">微信推送</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="链接目标">
                        <el-input v-model="addForm.linkTarget" placeholder="请输入链接目标"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属分类" prop="classifyId">
                        <el-select
                            v-model="addForm.classifyId"
                            placeholder="请选择"
                            style="width:100%"
                        >
                            <el-option
                                v-for="item in typeOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="24">
                    <el-form-item label="投入学校" prop="schoolId">
                        <div class="school-box clearfix">
                            <div class="slect-school fl">
                                <ul class="clearfix">
                                    <li v-for="(item,index) in selectSchoolList" :key="item.id">
                                        <img :src="item.logo" alt />
                                        <p class="name">{{item.name}}</p>
                                        <p class="close" @click="delSchool(index)">x</p>
                                    </li>
                                </ul>
                            </div>

                            <div class="add-school fl" @click="toSelectSchool">+</div>
                        </div>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="模块描述" prop="description">
                <el-input
                    type="textarea"
                    :rows="2"
                    v-model="addForm['description']"
                    placeholder="请输入模块描述"
                    clearable
                />
            </el-form-item>
            <el-form-item label="简短的描述" prop="shortDesc">
                <el-input
                    type="textarea"
                    :rows="2"
                    v-model="addForm['shortDesc']"
                    placeholder="请输入简短的描述"
                    clearable
                />
            </el-form-item>

            <el-form-item style="text-align:center">
                <el-button @click="$router.go(-1)" style="padding:10px 40px">取 消</el-button>
                <el-button
                    type="primary"
                    style="padding:10px 40px"
                    @click="addAndEditSubmit('ruleForm')"
                >保存</el-button>
            </el-form-item>
        </el-form>
        <schollDialog ref="school" @scelecSchool="scelecSchool" />
    </div>
</template>
<script type="text/ecmascript-6">
import API from '@/api';
import upload from '../../components/upLoad';
import schollDialog from '../../components/schoolSelect/index';
import cloneDeep from 'lodash/cloneDeep';
export default {
    components: {
        upload,
        schollDialog
    },
    data() {
        return {
            dialogVisible: false,
            dialogTitle: '新增',
            addForm: {
                name: '',
                iosIcon: '',
                androidIcon: '',
                pushType: '',
                accountTypeSupport: [],
                linkType: 'NATIVE',
                description: '',
                shortDesc: '',
                status: 'ON_SHELF',
                classifyId: '',
                schoolId: '',
                code:''
            },
            typeOptions: [],
            type: 1,
            dataRule: {
                name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
                linkType: [{ required: true, message: '模块类型不能为空', trigger: 'blur' }],
                code: [{ required: true, message: '模块编码不能为空', trigger: 'blur' }]
            },
            selectSchoolList: []
        };
    },
    watch:{
        selectSchoolList(val) {
            console.log(val)
            var schoolId = []
            for (var k in val) {
                schoolId.push(val[k].schoolId)
            }
            this.addForm.schoolId= schoolId.join(",")
        }
    },
    mounted() {
        this.type = this.$route.query.type;
        if (this.type == 2) {
            this.getEditData();
        }

        this.getClassifyList();
    },
    methods: {
        // 选中的学校数据
        scelecSchool($event) {
            console.log($event);
            this.selectSchoolList = [...this.selectSchoolList,...$event];
        },
        toSelectSchool() {
            this.$refs.school.init();
        },
        handleClose() {
            this.clearForm();
            this.dialogVisible = false;
        },
        getEditData() {
            API.menu.getEditFunctionMangerDetail({ id:this.$route.query.id }).then(res => {
                if (res.code == 20000000) {
                    var list = res.data
                   
                    this.addForm = {
                        name: list.name,
                        id: list.moduleId,
                        iosIcon: list.iosIcon,
                        androidIcon: list.androidIcon,
                        pushType: list.pushType,
                        accountTypeSupport: list.accountTypeSupport.split(","),
                        linkType: list.linkType==1 ?'NATIVE':'H5',
                        description: list.description,
                        shortDesc: list.shortDesc,
                        status: list.status==1 ? 'ON_SHELF' :'OFF_SHELF',
                        classifyId: list.classifyId,
                        schoolId: '',
                        code:list.code
                    };
                     this.selectSchoolList = list.schoolId
                }
            });
        },

        // 清空新增中的数据
        clearForm() {
            this.addForm = {
                name: '',
                iosIcon: '',
                androidIcon: '',
                pushType: '',
                accountTypeSupport: [],
                linkType: 'NATIVE',
                description: '',
                shortDesc: '',
                status: 'OFF_SHELF',
                classifyId: '',
                schoolId: ''
            };
        },
        // 获取所属分类数据
        getClassifyList() {
            this.listLoading = true;
            API.menu.getFunctionClassifyList({ pageNum: 1, pageSize: 1000 }).then(res => {
                this.listLoading = false;
                this.typeOptions = res.data.rows;
            });
        },
        // 删除学校
        delSchool(index) {
            var data = cloneDeep(this.selectSchoolList);
            data.splice(index, 1);
            this.selectSchoolList = data;
        },
        // 新增或编辑
        addAndEditSubmit(formName) {
            console.log(this.addForm.accountTypeSupport)
            var parm = cloneDeep(this.addForm)
            if (parm['accountTypeSupport'].length>0) {
                parm['accountTypeSupport'] = parm['accountTypeSupport'].join(",")
            } else {
                parm['accountTypeSupport'] = ''
            }
            
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 新增
                    if (this.type == 1) {
                        API.menu.addFunctionManger(parm).then(res => {
                            if (res.code == 20000000) {
                                this.$message({
                                    type: 'success',
                                    message: '新增成功'
                                });
                                this.clearForm();
                                this.$emit('refreshDataList');
                                this.dialogVisible = false;
                                this.$router.go(-1)
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    } else {
                        // 编辑
                        API.menu.editFunctionManger(parm).then(res => {
                            if (res.code == 20000000) {
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功'
                                });
                                this.clearForm();
                                this.$emit('refreshDataList');
                                this.dialogVisible = false;
                                this.$router.go(-1)
                            } else {
                                this.$message.error(res.message);
                            }
                        });
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
};
</script>
<style  scoped>
.add-school {
    width: 70px;
    height: 70px;
    line-height: 60px;
    text-align: center;
    font-size: 40px;
    float: left;
    cursor: pointer;
    color: #e4e4e4;
    border: 2px dashed #e4e4e4;
    margin-left: 20px;
}
.slect-school ul li {
    float: left;
    margin-right: 10px;
    width: 120px;
    height: 70px;
    padding: 6px;
    cursor: pointer;
    border: 1px solid #fff;
    margin-bottom: 20px;
    position: relative;
    text-align: center;
}
.slect-school ul li:hover {
    border: 1px solid #e4e4e4;
}
.slect-school ul li img {
    width: 40px;
    height: 40px;
}
.slect-school ul li p.name {
    font-size: 12px;
    line-height: 120%;
}
.slect-school ul li p.close {
    position: absolute;
    width: 18px;
    height: 18px;
    background: red;
    color: #fff;
    border-radius: 50%;
    right: 0;
    top: -6px;
    line-height: 18px;
    text-align: center;
}
</style>